package com.example.mybatisplus.controller;

import com.example.mybatisplus.common.JsonResponse;
import com.example.mybatisplus.mapper.RecordLogMapper;
import com.example.mybatisplus.model.domain.ConsultMerge;
import com.example.mybatisplus.model.domain.DictData;
import com.example.mybatisplus.model.domain.RecordLog;
import com.example.mybatisplus.service.RecordLogService;
import com.example.mybatisplus.service.SingleTimeService;
import com.example.mybatisplus.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2022-05-29
 * @version v1.0
 */
@Controller
@RequestMapping("/api/recordLog")
public class RecordLogController {

    private final Logger logger = LoggerFactory.getLogger( RecordLogController.class );

    @Autowired
    private RecordLogService recordLogService;
    @Autowired
    private RecordLogMapper recordLogMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private SingleTimeService singleTimeService;
    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id)throws Exception {
        RecordLog  recordLog =  recordLogService.getById(id);
        return JsonResponse.success(recordLog);
    }

    /**
    * 描述：根据Id删除
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        recordLogService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
    * 描述：根据Id 更新
    *
    */
    @RequestMapping(value = "/updateRecordLog", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse updateRecordLog(RecordLog  recordLog) throws Exception {
        recordLogService.updateById(recordLog);
        return JsonResponse.success(null);
    }


    /**
    * 描述:创建RecordLog
    *
    */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(RecordLog  recordLog) throws Exception {
        recordLogService.save(recordLog);
        return JsonResponse.success(null);
    }

    /**
     * 描述:创建add8records
     *
     */
    @RequestMapping(value = "/add8records", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse add8records(Long stu_id) throws Exception {

        ConsultMerge consultMerge = recordLogService.queryConsultStuid(stu_id);

        boolean isExist = recordLogService.isExist(stu_id);
        if(isExist){
            return JsonResponse.failure(null);
        }else {
            for (int i = 0;i < 8;i++){
                consultMerge.setCurrentWeek(i+1);
                recordLogService.add8records(consultMerge);
            }
            return JsonResponse.success(null);

        }

    }

    /**
     * 描述： 查询所有
     *
     */
    @RequestMapping(value = "/getall", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getall()throws Exception {
        List<RecordLog> logList=recordLogService.getall();
        List<Map<String,String>> maps=new ArrayList<>();
        for (RecordLog recordLog:logList){
            maps.add(packgeRecordLog(recordLog));
        }
        return JsonResponse.success().setData(maps);
    }


    /**
     * 描述：根据stuId docId currentweek 更新
     *
     */
    @RequestMapping(value = "/changeRecord", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse changeRecord(RecordLog  recordLog) throws Exception {
        recordLogService.changeRecord(recordLog);
        return JsonResponse.success(null).setMessage("谢谢牛皮");
    }

    @RequestMapping(value = "/changeRecordxxn", method = RequestMethod.PUT)
    @ResponseBody
    public JsonResponse changeRecordfasgfas(@Param("id") Long id,@Param("status") Long status) throws Exception {
        recordLogService.changeRecordxxn(id,status);
        return JsonResponse.success(null).setMessage("谢谢牛皮");
    }


    /**
     * 描述:创建add1record
     *
     */
    @RequestMapping(value = "/add1record", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse add1record(Long stu_id) throws Exception {

        ConsultMerge consultMerge = recordLogService.queryConsultStuid(stu_id);
        Integer newweek = recordLogService.queryMaxcurrentweek(stu_id);
        consultMerge.setCurrentWeek(newweek);//数据库查询获得当前周并且++
        recordLogService.updateEndDate(stu_id);
        recordLogService.updateConsultCount(stu_id);//同步consultmerge表中的咨询次数
        recordLogService.add8records(consultMerge);//注意，这里不是增加八条记录，只是方便使用，这里只增加一条记录
        return JsonResponse.success("成功");
    }


    public Map<String,String> packgeRecordLog(RecordLog recordLog){
        Map<String,String> map=new HashMap<String, String>();
        try {
            map.put("id",recordLog.getId().toString());
            map.put("currentWeek",recordLog.getCurrentWeek().toString());
            map.put("recordStatusString",recordLogService.getrecordStatusString(recordLog.getRecordStatus()));
            map.put("stu_name",userService.getById(recordLog.getStuId()).getStuName());
            map.put("tea_name",userService.getConsultantById(recordLog.getDocId()).getStuName());
            map.put("week_name",singleTimeService.getById(recordLog.getTimeId()).getWeakDay());
            map.put("detail_time",singleTimeService.getById(recordLog.getTimeId()).getTimeDay());
            map.put("teaId",recordLog.getDocId().toString());
            map.put("stuId",recordLog.getStuId().toString());

        } catch (Exception e){
            return map;
        }
//        map.put("consultantId",consultMerge.getConsultantId().toString());
//        map.put("startDate",consultMerge.getStartDate().toString());
//        map.put("endDate",consultMerge.getEndDate().toString());
        return map;
    }

    /**
     * 描述：根据Id 查询
     *
     */
    @RequestMapping(value = "/getdicttypefour", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getdicttypefour()throws Exception {
        List<DictData> dictData =recordLogMapper.getdicttypefour();
        return JsonResponse.success().setData(dictData);
    }
}



